package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.VideoView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.imgedit.view.StickerView;
import com.stark.imgedit.view.TextStickerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import flc.ast.BaseAc;
import flc.ast.BaseEditVideoFragment;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.bean.VideoFuncBean;
import flc.ast.databinding.ActivityVideoEditBinding;
import flc.ast.fragment.VideoMusicFragment;
import flc.ast.fragment.VideoSpeedFragment;
import flc.ast.fragment.VideoTailorFragment;
import flc.ast.fragment.VideoTextFragment;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.TimeUtil;

public class VideoEditActivity extends BaseAc<ActivityVideoEditBinding> {

    public static String mEditVideoPath;

    public static final int MODE_NONE = 0;
    public static final int MODE_TAILOR = 1;  // 裁剪模式
    public static final int MODE_MUSIC = 2;  // 音乐模式
    public static final int MODE_TEXT = 3;  // 文字模式
    public static final int MODE_SPEED = 4;  // 变速模式

    private List<VideoFuncBean> mFuncItems;

    public VideoView mVideoView;
    public ImageView mImageView;
    public String mPlayVideo = "";
    public TextStickerView mTextStickerView;

    public int mode = MODE_NONE;// 当前操作模式

    private VideoTailorFragment mVideoTailorFragment;
    private VideoSpeedFragment mVideoSpeedFragment;
    private VideoTextFragment mVideoTextFragment;
    private VideoMusicFragment mVideoMusicFragment;

    @Override
    protected int onCreate() {
        return R.layout.activity_video_edit;
    }

    @Override
    public void onBackPressed() {
        switch (mode) {
            case MODE_TAILOR:
                mVideoTailorFragment.backToMain();
                return;
            case MODE_MUSIC:
                mVideoMusicFragment.backToMain();
                return;
            case MODE_SPEED:
                mVideoSpeedFragment.backToMain();
                return;
            case MODE_TEXT:
                mVideoTextFragment.backToMain();
                return;
            default:
                break;
        }
        super.onBackPressed();
    }

    @Override
    protected void initView() {
        mDataBinding.ivVideoEditBack.setOnClickListener(v -> onBackPressed());

        mVideoView = mDataBinding.videoView;
        mImageView = mDataBinding.ivPlayStart;
        mTextStickerView = mDataBinding.txtStickerView;

        mDataBinding.tvVideoEditSave.setOnClickListener(this);
        mDataBinding.ivApply.setOnClickListener(this);
        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.ivVideoTailor.setOnClickListener(this);
        mDataBinding.ivVideoMusic.setOnClickListener(this);
        mDataBinding.ivVideoText.setOnClickListener(this);
        mDataBinding.ivVideoSpeed.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvVideoEditSave://保存
                saveVideo();
                break;
            case R.id.ivCancel://取消
                onBackPressed();
                break;
            case R.id.ivApply://确定
                clickApply();
                break;
            case R.id.ivVideoTailor://剪切
                showFragmentByFunc(mFuncItems.get(0));
                break;
            case R.id.ivVideoMusic://音乐
                showFragmentByFunc(mFuncItems.get(1));
                break;
            case R.id.ivVideoText://文字
                showFragmentByFunc(mFuncItems.get(2));
                break;
            case R.id.ivVideoSpeed://变速
                showFragmentByFunc(mFuncItems.get(3));
                break;
        }
    }

    @Override
    protected void initData() {
        mFuncItems = getFuncData();
        setPlayer();
    }

    //保存视频
    private void saveVideo() {
        showDialog(getString(R.string.saving));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        FileP2pUtil.copyPrivateVideoToPublic(mContext, mEditVideoPath);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.save_success));
                    }
                });
            }
        }, 1000);
    }

    private void showFragmentByFunc(VideoFuncBean videoFuncBean) {
        if (videoFuncBean == null) {
            return;
        }

        showEdit(videoFuncBean.getName());
        switch (videoFuncBean.getFunc()) {
            case VideoTailorFragment.INDEX:
                showTailorFragment();
                break;
            case VideoSpeedFragment.INDEX:
                showSpeedFragment();
                break;
            case VideoTextFragment.INDEX:
                showTextFragment();
                break;
            case VideoMusicFragment.INDEX:
                showMusicFragment();
                break;
            default:
                break;
        }
    }

    //设置播放器
    private void setPlayer() {
        mDataBinding.videoView.setVideoPath(mEditVideoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mDataBinding.ivPlayStart.setImageResource(R.drawable.abof);
                mDataBinding.videoView.seekTo(1);
            }
        });
        //直接播放视频
        mDataBinding.videoView.seekTo(1);
        mDataBinding.ivPlayStart.setImageResource(R.drawable.azant);
        mDataBinding.videoView.start();
    }

    /**
     * 点击右下角“勾”后的处理
     */
    private void clickApply() {
        switch (mode) {
            case MODE_TAILOR:
                mVideoTailorFragment.applyTailorVideo();
                break;
            case MODE_MUSIC:
                mVideoMusicFragment.applyMusicVideo();
                break;
            case MODE_SPEED:
                mVideoSpeedFragment.applySpeedVideo();
                break;
            case MODE_TEXT:
                mVideoTextFragment.applyTextVideo();
                break;
            default:
                break;
        }
    }

    private void showTailorFragment() {
        if (mVideoTailorFragment == null) {
            mVideoTailorFragment = VideoTailorFragment.newInstance();
        } else {
            mVideoTailorFragment.onShow();
        }
        showFragment(mVideoTailorFragment);
    }


    private void showEdit(String name) {
        mDataBinding.tvTitle.setText(name);
        mDataBinding.llVideoEditBottom.setVisibility(View.GONE);
        mDataBinding.flFragmentContainer.setVisibility(View.VISIBLE);
        mDataBinding.rlVideoEdit.setVisibility(View.VISIBLE);
    }

    public void showFunc() {
        mDataBinding.llVideoEditBottom.setVisibility(View.VISIBLE);
        mDataBinding.flFragmentContainer.setVisibility(View.INVISIBLE);
        mDataBinding.rlVideoEdit.setVisibility(View.GONE);
    }

    public String getMainPath() {
        return mEditVideoPath;
    }

    public void changeMainPath(String newPath, boolean needPushUndoStack) {
        if (TextUtils.isEmpty(newPath)) {
            return;
        }

        if (!mEditVideoPath.equals(newPath)) {
            mEditVideoPath = newPath;
            mVideoView.setVideoPath(mEditVideoPath);
            mVideoView.seekTo(1);
            mVideoView.start();
            mImageView.setImageResource(R.drawable.azant);
        }
    }


    private void showSpeedFragment() {
        if (mVideoSpeedFragment == null) {
            mVideoSpeedFragment = VideoSpeedFragment.newInstance();
        } else {
            mVideoSpeedFragment.onShow();
        }
        showFragment(mVideoSpeedFragment);
    }

    private void showTextFragment() {
        if (mVideoTextFragment == null) {
            mVideoTextFragment = VideoTextFragment.newInstance();
        } else {
            mVideoTextFragment.onShow();
        }
        showFragment(mVideoTextFragment);
    }

    private void showMusicFragment() {
        if (mVideoMusicFragment == null) {
            mVideoMusicFragment = VideoMusicFragment.newInstance();
        } else {
            mVideoMusicFragment.onShow();
        }
        showFragment(mVideoMusicFragment);
    }

    private void showFragment(BaseEditVideoFragment baseEditFragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.flFragmentContainer, baseEditFragment);
        fragmentTransaction.commit();
    }


    private List<VideoFuncBean> getFuncData() {
        if (mFuncItems != null) {
            return mFuncItems;
        }
        List<VideoFuncBean> funcBeans = new ArrayList<>();
        mFuncItems = funcBeans;
        VideoFuncBean funcBean = null;
        funcBean = new VideoFuncBean(VideoFuncBean.FUNC_TAILOR, getString(R.string.video_tailor), R.drawable.ajianji);
        funcBeans.add(funcBean);
        funcBean = new VideoFuncBean(VideoFuncBean.FUNC_MUSIC, getString(R.string.video_music), R.drawable.ayinyue);
        funcBeans.add(funcBean);
        funcBean = new VideoFuncBean(VideoFuncBean.FUNC_TEXT, getString(R.string.video_text1), R.drawable.awenzi);
        funcBeans.add(funcBean);
        funcBean = new VideoFuncBean(VideoFuncBean.FUNC_SPEED, getString(R.string.video_speed), R.drawable.abiansu);
        funcBeans.add(funcBean);
        return funcBeans;
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //直接播放视频
        mDataBinding.videoView.seekTo(1);
        mDataBinding.ivPlayStart.setImageResource(R.drawable.azant);
        mDataBinding.videoView.start();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        FileUtils.delete(PathUtils.getFilesPathExternalFirst() + Content.app_tmp_video);
    }

}